<template>
	<view class="container">
		<view class="header">
			<image src="@/static/admin/missing-face.png" mode="aspectFit" class="image"></image>
		</view>
		<!-- <view class="title">申请获取以下权限</view>
		<view class="subtitle">获得你的公开信息（昵称、头像等）</view> -->
		<view class="login-btn">
			<!-- <button @click="login" open-type="login">授权登录</button> -->
			<button open-type="getPhoneNumber" @getphonenumber="getphonenumber" v-if="token">授权登录</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				token:''
			}
		},
		onShow() {
			this.token = uni.getStorageSync("token")
			if (this.token) {
				this.getUserinfo()
			}else{
				this.login()
			}
		},
		methods: {
			login(e) {
				this.$loanding("登录中...")
				uni.login({
					provider: 'weixin',
					success: (res) => {
						this.wxlogin(res.code)
					},
					fail: (e) => {
						uni.showModal({
							title: '提示',
							content: '授权失败，请重新授权',
							showCancel: false,
							success: function(res) {}
						})
					}
				})
			},
			wxlogin(code) {
				const _this = this
				this.$post('/user/wxlogin', {
					code: code,
				}).then((res) => {
					this.$toast(res.msg)
					if (res.code == 200) {
						uni.setStorage({
							key: 'token',
							data: res.data.token
						})
						_this.token = res.data.token
						_this.getUserinfo(res.data.token)

					}
				})
			},
			getUserinfo() {
				this.$post("/user/userinfo", {}).then(res => {
					//没有手机号，去绑定手机号
					if (res.data.post_id == 0 || res.data.department_id == 0) {
						// uni.redirectTo({
						// 	url: "/pages/login/phone"
						// })
					} else if (res.data.group_id == 1) {
						//工人
						uni.redirectTo({
							url: "/pages/staff/index/index"
						})
					} else if (res.data.group_id == 2) {
						//管理员
						uni.redirectTo({
							url: "/pages/admin/index/index"
						})
					}
				})
			},
			getphonenumber(e) {
				if (e.detail.code) {
					console.log(e.detail.code)
					this.$post("/user/mobile_auth", {
						code: e.detail.code
					}).then(res => {
						if (res.code == 200) {
							this.getUserinfo()
						}else{
							this.$toast(res.msg)
						}
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #fff;
		font-size: 32rpx;
		text-align: center;
	}

	.container {
		padding: 0 60rpx;
	}

	.header {
		padding: 80rpx 0 50rpx;

		image {
			width: 200rpx;
			height: 200rpx;
		}
	}

	.title {
		color: #585858;
		font-size: 34rpx;
		margin-bottom: 40rpx;
	}

	.subtitle {
		color: #888;
		margin-bottom: 88rpx;
		font-size: 28rpx;
	}

	.login-btn {
		padding: 0 20rpx;
	}

	button::after {
		border: none;
	}

	.login-btn button {
		height: 88rpx;
		line-height: 88rpx;
		background: $main-color;
		color: #fff;
		font-size: 32rpx;
		border-radius: 16rpx;
		text-align: center;
	}
</style>